import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hong_vpn_new_20240626/hvn_tools/vpn_manager.dart';
import 'package:hong_vpn_new_20240626/utils/configs/color_config.dart';
import 'package:hong_vpn_new_20240626/utils/configs/text_style_config.dart';
import 'package:hong_vpn_new_20240626/utils/customs/zz_button.dart';
import 'package:hong_vpn_new_20240626/utils/zz_navigator.dart';

import '../../utils/api.dart';
import '../../utils/global.dart';

class LocationListWidget extends StatefulWidget {
  const LocationListWidget({super.key});

  @override
  State<LocationListWidget> createState() => _LocationListWidgetState();
}

class _LocationListWidgetState extends State<LocationListWidget> {
  late bool isLoading = false;
  @override
  void initState() {
    super.initState();
  }

  reloadVpn() async {
    isLoading = true;
    setState(() {});
    try {
      await API.reloadVpnList();
      // ignore: empty_catches
    } catch (e) {}
    isLoading = false;
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Locations")),
      body: Global.vpns.isEmpty
          ? Container(
              alignment: Alignment.center,
              padding: EdgeInsets.only(bottom: 100.h),
              child: isLoading
                  ? const CupertinoActivityIndicator(radius: 20)
                  : Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          "It's empty",
                          style: TextStyleConfig.bold.size(20),
                        ),
                        const SizedBox(height: 20),
                        TextButton(
                            onPressed: () => reloadVpn(),
                            child: Text(
                              "Click to refresh",
                              style: TextStyleConfig.middle,
                            ))
                      ],
                    ),
            )
          : ListView.builder(
              itemCount: Global.vpns.length,
              itemBuilder: (BuildContext context, int index) {
                return _cellItem(index);
              },
            ),
    );
  }

  Widget _cellItem(int index) {
    var item = Global.vpns[index];
    return GestureDetector(
      onTap: () {
        Global.currVpn = item;
        setState(() {});
        VpnManager.share.connect();
        ZZNavigator.pop();
      },
      child: Container(
        color: ColorConfig.bg,
        alignment: Alignment.center,
        height: 50,
        margin: const EdgeInsets.only(left: 30, right: 30),
        child: Column(
          children: [
            Expanded(
                child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  item.hotName,
                  style: TextStyleConfig.middle,
                ),
                const Spacer(),
                ZZButton(
                  isSelected: item == Global.currVpn,
                  image: Image.asset("assets/images/未选中.png"),
                  selImage: Image.asset("assets/images/选中.png"),
                )
              ],
            )),
            Divider(
              height: 0.5,
              thickness: 0,
              color: Colors.white.withOpacity(0.2),
            )
          ],
        ),
      ),
    );
  }
}
